home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / comnumb.exe / RATIONAL.H < prev    next >
C/C++ Source or Header  |  1992-05-01  |  2KB  |  60 lines

  1. ////////////////////////////////////////////////////////////////
  2. // rational.h: Rational number class definition.
  3. // Copyright(c) 1992 Azarona Software. All rights reserved.
  4. ////////////////////////////////////////////////////////////////
  5. #ifndef H_RATIONAL
  6. #define H_RATIONAL
  7. #include <iostream.h>
  8.  
  9. class Rational {
  10. private:
  11.   long n, d; // Numerator and denominator
  12. public:
  13.   // Constructors and assignment
  14.   Rational();
  15.   Rational(long u);
  16.   Rational(long u, long v);
  17.   Rational(const Rational &r);
  18.   Rational &operator=(const Rational &r);
  19.   // Helper functions
  20.   long Numerator() const;
  21.   long Denominator() const;
  22.   void Set(long u, long v);
  23.   void Simplify();
  24.   void Invert();
  25.   void Negate();
  26.   long RemoveWholePart();
  27.   int IsUndefined() const;
  28.   int IsPositive() const;
  29.   int IsNegative() const;
  30.   int IsZero() const;
  31.   // Stream I/O operators
  32.   friend istream &operator>>(istream &s, Rational &r);
  33.   friend ostream &operator<<(ostream &s, const Rational &r);
  34.   // Arithmetic operators that modify their operand
  35.   Rational operator++(int); // Postfix
  36.   Rational operator--(int); // Postfix
  37.   Rational &operator++();   // Prefix
  38.   Rational &operator--();   // Prefix
  39.   Rational &operator+=(const Rational &r);
  40.   Rational &operator-=(const Rational &r);
  41.   Rational &operator*=(const Rational &r);
  42.   Rational &operator/=(const Rational &r);
  43.   // Arithmetic operators resulting in new object
  44.   Rational operator-() const;
  45.   Rational operator+() const;
  46.   friend Rational operator*(const Rational &a, const Rational &b);
  47.   friend Rational operator/(const Rational &a, const Rational &b);
  48.   friend Rational operator+(const Rational &a, const Rational &b);
  49.   friend Rational operator-(const Rational &a, const Rational &b);
  50.   // Comparison operators
  51.   friend int operator==(const Rational &a, const Rational &b);
  52.   friend int operator!=(const Rational &a, const Rational &b);
  53.   friend int operator<(const Rational &a, const Rational &b);
  54.   friend int operator<=(const Rational &a, const Rational &b);
  55.   friend int operator>(const Rational &a, const Rational &b);
  56.   friend int operator>=(const Rational &a, const Rational &b);
  57. };
  58.  
  59. #endif
  60.